package com.kehutong.help.service;

import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.mvc.webservice.WebService;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.execute.entity.FindPage;
import org.coraframework.util.Objects;
import com.kehutong.help.entity.Help;
import com.kehutong.help.entity.HelpClassify;

@WebService("/navigation/service")
public class HelpService {

    @Inject
    private JdbcSession jdbcSession;

    public Object list(JSONObject jsonObject) {
        FindPage<Help> findPage = jdbcSession.findPage(Help.class);
        if(jsonObject.getBooleanValue("showStatus")) {
            findPage.eq("showStatus", true);
        }

        return findPage.eq(jsonObject, "classifyId")
            .like(jsonObject, "title")
            .eq("deleted", false)
            .order("showStatus", "desc")
            .order("sort", "asc")
            .page(jsonObject)
            .exe();
    }

    public Object save(Help help) {
        setClassifyName(help);

        jdbcSession.insert(help);

        return Result.success();
    }

    private void setClassifyName(Help help) {
        HelpClassify classify = Pool.get(HelpClassify.class, help.getClassifyId());
        if (Objects.nonNull(classify)) {
            help.setClassifyName(classify.getName());

            Help old = Pool.get(Help.class, help.getId());
            if (Objects.isNull(old)){
                int count = classify.getCount() + 1;
                classify.setCount(count);
                jdbcSession.updateById(classify);
            } else if (!help.getClassifyId().equals(old.getClassifyId())){
                int count = classify.getCount() + 1;
                classify.setCount(count);
                jdbcSession.updateById(classify);

                HelpClassify oldClassify = Pool.get(HelpClassify.class, old.getClassifyId());
                if (Objects.nonNull(oldClassify)) {
                    int oldCount = oldClassify.getCount() - 1;
                    oldClassify.setCount(oldCount);
                    jdbcSession.updateById(oldClassify);
                }
            } else {
                // continue
            }

        }
    }

    public Object update(Help help) {
        setClassifyName(help);
        jdbcSession.updateById(help);

        return Result.success();
    }

    public Object delete(Help help) {
        help.setDeleted(true);
        jdbcSession.updateById(help);

        HelpClassify classify = Pool.get(HelpClassify.class, help.getClassifyId());
        int count = classify.getCount() - 1;
        classify.setCount(count);
        jdbcSession.updateById(classify);

        return Result.success();
    }

    public Object detail(Help help) {
        if (Objects.isNull(help)) {
            return com.kehutong.common.errcode.Result.error(0, "当前信息不存在");
        }

        // 阅读数+1
        int readNum = help.getReadNum() + 1;
        help.setReadNum(readNum);
        jdbcSession.updateById(help);

        return Result.success(help);

    }

}
